package com.java.java网络.exercise;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * InetAddress类的方法使用：getLocalHost获取本机信息，getByName指定主机名称获取，getAllByName根据域名获取主机信息
 * @create 2021-04-05 20:06
 */
public class NetTwo {
    public static void main(String[] args) throws UnknownHostException {
        // 获取本机InetAddress类 : 获取设备名称
        System.out.println(java.net.InetAddress.getLocalHost()); // DESKTOP-V96NSE6/192.168.0.124

        // 2.根据指定主机名称获取InetAddress对象
//        InetAddress byName = InetAddress.getByName("DESKTOP-S4MP84S");
        InetAddress byName = InetAddress.getByName("DESKTOP-V96NSE6");
        System.err.println(byName);
        // 3.根据域名获取InetAddress对象
        for (InetAddress inetAddress : InetAddress.getAllByName("www.baidu.com")) {
            System.out.println("百度：" + inetAddress);
        }
        // 4.通过InetAddress对象获取IP地址
        String hostAddress = byName.getHostAddress();
        // 4.通过InetAddress对象获取IP地址
        String hostName = byName.getHostName();
        System.out.println("本机地址ip:" + hostAddress);
        System.out.println("本机名:" + hostName);
    }
}
